<?php

class Sfs_Filesystem_Ooo  extends Sfs_Filesystem_Zip{

    /**
     * @var string 路徑
     */
    protected $_path='';


    /**
     *
     * @param string  $path  Ooo 檔案位置
     */
    public function __construct ($path ='')
    {

        if ($path)
        $this->_path = $path;
    }

    /**
     *  設定 ooo 路徑
     * @param unknown_type $path
     */
    public function setPath($path)
    {
        $this->_path = $path;
    }

    /**
     * 取出 ooo 檔案路
     * @retuen string
     */
    public function getPath()
    {
        return $this->_path;
    }


    /**
     *  預載檔案
     */
    protected function _addPrepareFile()
    {
        if (is_dir($this->_path)) {
            if (($dh = opendir($this->_path)) == true) {
                while (($file = readdir($dh)) !== false) {
                    if(in_array($file,$this->_excludeArr)){
                        continue;
                    }elseif(is_dir($this->_path."/".$file)){
                        $this->add_dir($this->_path."/$file/",$file."/");
                    }
                    else{
                        if (! in_array($file, $this->_skipFiles))
                        $this->add_file(file_get_contents($this->_path."/".$file),$file);
                    }
                }
                closedir($dh);
            }
        }
        else
        throw new Zend_Exception('Error '.$this->_path. ' is not a directory');
    }

    /**
     *  取出檔案內容
     * @param string $file_name 檔名
     * @param  string $dir 目錄
     */
    public function getContent($file_name,$dir='')
    {
        if ($dir !='')
        $file = $dir.'/'.$file_name;
        else
        $file = $this->_path.'/'.$file_name;
        if (is_file($file))
        return file_get_contents($file);
        else
        throw new Zend_Exception('Error Filename: '.$file);
    }

    public function add_file($file_data, $name, $timestamp = '')
    {
        $this->_excludeArr[] = $name;
        parent::add_file($file_data, $name, $timestamp);
    }


    public function send_odt($filename)
    {
        $this->_addPrepareFile();
        parent::send_odt($filename);
    }
}